﻿using Common;
using Common.Helper;
using enterprise.Website.Core.Filter;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace enterprise.Website.Core.ApiControllers
{
    /// <summary>
    /// 文件上传控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [LoginAuthorize]
    public class UploadController : ControllerBase
    {
        private readonly IWebHostEnvironment _hostingEnvironment;


        public UploadController(IWebHostEnvironment hostingEnvironment)
        {
            _hostingEnvironment = hostingEnvironment;
        }
        /// <summary>
        /// 后台上传文件接口
        /// </summary>
        /// <param name="isNewFileName">是否重置文件名</param>
        /// <returns></returns>
        [HttpPost("manageUpload")]
        public ApiResult<object> manageUpload([FromForm]bool isNewFileName = true)
        {
            var result = new ApiResult<object> { Success = false, Message = "上传失败" };
            var file = Request.Form.Files.FirstOrDefault();
            string saveRoute = AppSetting.UploadRoute;
            var MaxFile = AppSetting.MaxFile;
            if ((MaxFile * (1024 * 1024)) < file.Length)
            {
                result.Success = false;
                result.Message = $"上传的文件过大，最大仅支持[{MaxFile}MB]";
                return result;
            }
            if (file != null)
            {
                string rootPath = Path.Combine(_hostingEnvironment.WebRootPath, saveRoute);
                if (!Directory.Exists(rootPath))//不存在就创建目录
                {
                    Directory.CreateDirectory(rootPath);
                }
                string FileName = isNewFileName ? Guid.NewGuid().ToString() + Path.GetExtension(file.FileName) : file.FileName;
                var filePath = Path.Combine(rootPath, FileName);
                if (System.IO.File.Exists(filePath))//如果有同名文件，则进行重命名
                {
                    FileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
                    filePath = Path.Combine(rootPath, FileName);
                }
                using (var stream = System.IO.File.Create(filePath))//文件保存
                {
                    file.CopyTo(stream);
                }
                result.Success = true;
                result.Message = "上传成功";
                result.Data = new { src = Path.Combine("/" + saveRoute, FileName) };
            }
            return result;
        }


        /// <summary>
        /// 编辑器上传接口
        /// </summary>
        /// <returns></returns>
        [HttpPost("CKEFileUpload")]
        public object CKEFileUpload()
        {
            var file = Request.Form.Files.FirstOrDefault();
            string saveRoute = AppSetting.UploadCKEDITOR;
            var MaxFile = AppSetting.MaxFile;
            if ((MaxFile * (1024 * 1024)) < file.Length)
            {
                return new { uploaded = 0, success = false, error = new { message = $"上传的文件过大，最大仅支持[{MaxFile}MB]" } };
            }
            if (file != null)
            {
                string rootPath = Path.Combine(_hostingEnvironment.WebRootPath, saveRoute);
                if (!Directory.Exists(rootPath))//不存在就创建目录
                {
                    Directory.CreateDirectory(rootPath);
                }
                string FileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
                var filePath = Path.Combine(rootPath, FileName);
                if (System.IO.File.Exists(filePath))//如果有同名文件，则进行重命名
                {
                    FileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
                    filePath = Path.Combine(rootPath, FileName);
                }
                using (var stream = System.IO.File.Create(filePath))//文件保存
                {
                    file.CopyTo(stream);
                }
                return new { uploaded = 1, url = Path.Combine("/" + saveRoute, FileName) };
            }
            return new { uploaded = 0 };
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <returns></returns>
        [HttpDelete("DelFile")]
        public ApiResult DelFile([FromForm]string src)
        {
            var result = new ApiResult { Success = true, Message = "删除成功" };
            var filePath = _hostingEnvironment.WebRootPath + src;
            if (System.IO.File.Exists(filePath))//如果有文件，则进行删除
            {
                System.IO.File.Delete(filePath);
            }
            return result;
        }
    }
}
